#include "Pwm.h"


Pwm::Pwm()
{
	oneWidth = Counter<unsigned char>(0,0);
	zeroWidth = Counter<unsigned char>(0,0);
	state = 0;
	out = BitPointer();
}
Pwm::Pwm(unsigned char zero, unsigned char one, BitPointer o)
{
	oneWidth = Counter<unsigned char>(0, one);
	zeroWidth = Counter<unsigned char>(0, zero);
	out = o;
	state = o.getBit();
}
void Pwm::setWidth(unsigned char zero, unsigned char one)
{
	//resets the count for ones
	if(oneWidth.getCount() >= one)
		oneWidth = Counter<unsigned char>(0, one);
	else
		oneWidth = Counter<unsigned char>(oneWidth.getCount(), one);
	//resets the width for zero count.
	if(zeroWidth.getCount() >= zero)
		zeroWidth = Counter<unsigned char>(0, zero);
	else
		zeroWidth = Counter<unsigned char>(zeroWidth.getCount(), zero);
}

void Pwm::run()
{
	//determine which counter I need to increment
	if(bit.getBit()==0)
		zeroWidth++;
	else //bit == 1 increment one counter.
		oneWidth++;

	//now see if I can not the bit yet
	if(zeroWidth.flag() == true || oneWidth.flag() == true)
		bit.notBit(); //not the bit if either flag is true.

	//now reset the counters that have flaged true if any
	if(zeroWidth.flag() == true)
		zeroWidth.resetCount(0);
	else if(oneWidth.flag() == true)
		oneWidth.resetCount(0);
}

